import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../components/login.vue";
import Home from "../components/home.vue";
import Welcome from "../components/welcome.vue";
import Users from "../components/user/users.vue"
import Rights from "../components/power/rights.vue"
import Roles from "../components/power/roles.vue"
import Goods from "../components/shop/goods.vue"
import Params from "../components/shop/params.vue"
import Categories from "../components/shop/categories.vue"
import Orders from "../components/order/orders.vue"
import Reports from "../components/report/reports.vue"


Vue.use(VueRouter);

const routes = [
  {
    //重定向
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
  },
  {
    path: "/home",
    component: Home,
    redirect:'/Welcome',
    children: [
      {path: "/welcome",component: Welcome,},
      {path:'/users',component:Users},
      {path:'/rights',component:Rights},
      {path:'/roles',component:Roles},
      {path:'/goods',component:Goods},
      {path:'/params',component:Params},
      {path:'/categories',component:Categories},
      {path:'/orders',component:Orders},
      {path:'/reports',component:Reports},
    ],
  },
];

const router = new VueRouter({
  routes,
});

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to 将要访问的路径
  //from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // 两种方式： next() 直接放行  ； next('/login') 强制跳转
  if (to.path === "/login") return next();
  // 获取 token
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) return next("/login");
  next();
});

export default router;
